/*
** Copyright (c) 2008 Mobile GCalendar
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 *
 */
package au.edu.mq.comp.itec800.mgc.util;

import au.edu.mq.comp.itec800.mgc.MGC;
import au.edu.mq.comp.itec800.mgc.lib.GAccount;
import au.edu.mq.comp.itec800.mgc.ui.UserInterface;
import au.edu.mq.comp.itec800.mgc.ui.cli.Console;
import au.edu.mq.comp.itec800.mgc.ui.gui.Application;

/**
 * Convenience class containing static access methods for maintenance and
 * data manipulation operations for the Mobile GCalendar program.
 *
 * Those are mostly used to reduce the development strain to access
 * methods and variables of the MGC singleton class.
 *
 *
 * @see MGC
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 */
public class MGCUtils
{

  /**
   * Returns the MGC singleton object.
   *
   * @return
   *          the running instance of MGC.
   */
  public static MGC           getMGC()
  {
    return (MGC.getInstance());
  }

  /**
   * Returns the currently active account of the running instance of MGC, if
   * a user is already logged in. Returns null otherwise.
   *
   *
   * @return
   *           the active GAccount object.
   */
  public static GAccount      getGAccount()
  {
    return (getMGC().getActiveAccount());
  }

  /**
   * Returns the active UserInterface object, which has been instantiated
   * depending on the MGC configuration file and/or command-line options.
   *
   * @return
   *            the implementation object of UserInterface.
   */
  public static UserInterface getUI()
  {
    return (getMGC().getUI());
  }

  /**
   * Returns the Console object.
   * Beware that this method will produce a wrong class cast exception if
   * it is called while the MGC program is running in GUI mode.
   *
   * @return
   *            the Console object implementing UserInterface.
   */
  public static Console       getConsole()
  {
    return ((Console) getUI());
  }

  /**
   * Returns the Application object.
   * Beware that this method will produce a wrong class cast exception if
   * it is called while the MGC program is running in CLI mode.
   *
   * @return
   *            the Application object implementing UserInterface.
   */
  public static Application   getApplication()
  {
    return ((Application) getUI());
  }

  /**
   * Returns the MGC program's configuration.
   *
   * @return
   *            the Configuration object.
   */
  public static Configuration getConfiguration()
  {
    return (getMGC().getConfiguration());
  }

  public static String        query(final String q)
  {
    final String              clean = squery(q);

    return ((clean.length() == 0) ? null : clean);
  }

  public static String        squery(final String q)
  {
    return (StringUtils.sanitize(((Console) getUI()).getIO().input(q)));
  }

  public static String        query(final String q, String v)
  {
    v = (v != null) ? v.trim() : "";
    return ((v.length() > 0) ? v : squery(q));
  }

  public static String        query(final String q,
                                    final int i,
                                    final String ... v)
  {
    return (query(q, ((v != null) && (i >= 0) && (i < v.length)) ? v[i] : ""));
  }
}
